﻿namespace EF505178WorkAround
{
    using System.ComponentModel.Composition;
    using System.Linq;

    using Microsoft.Data.Entity.Design.Extensibility;

    [Export(typeof(IModelTransformExtension))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class ModelTransformExtension : IModelTransformExtension
    {
        void IModelTransformExtension.OnAfterModelLoaded(ModelTransformExtensionContext context)
        {
        }

        void IModelTransformExtension.OnBeforeModelSaved(ModelTransformExtensionContext context)
        {
            var doc = context.CurrentDocument;
            foreach (var element in
                doc.Descendants("{http://schemas.microsoft.com/ado/2008/09/edm}Property").Where(
                    x =>
                    x.Attribute("{http://schemas.microsoft.com/ado/2009/02/edm/annotation}StoreGeneratedPattern") !=
                    null &&
                    x.Attribute("{http://schemas.microsoft.com/ado/2009/02/edm/annotation}StoreGeneratedPattern").Value !=
                    "None"))
            {
                var entityTypeNamespace = element.Parent.Parent.Attribute("Namespace").Value;
                var entityTypeName = element.Parent.Attribute("Name").Value;
                var entityTypeFullName = entityTypeNamespace + "." + entityTypeName;
                var propertyName = element.Attribute("Name").Value;
                var storeGeneratedPatternValue = element.Attribute("{http://schemas.microsoft.com/ado/2009/02/edm/annotation}StoreGeneratedPattern").Value;

                var entityTypeMapping =
                    doc.Descendants("{http://schemas.microsoft.com/ado/2008/09/mapping/cs}EntityTypeMapping").Where(
                        x =>
                        x.Attribute("TypeName").Value == entityTypeFullName ||
                        x.Attribute("TypeName").Value == "IsTypeOf(" + entityTypeFullName + ")").First();
                var storeEntitySet =
                    entityTypeMapping.Element("{http://schemas.microsoft.com/ado/2008/09/mapping/cs}MappingFragment").
                        Attribute("StoreEntitySet").Value;

                var ssdlEntityType =
                    doc.Descendants("{http://schemas.microsoft.com/ado/2009/02/edm/ssdl}EntityType").Where(
                        x => x.Attribute("Name").Value == storeEntitySet).First();

                var ssdlProperty =
                    ssdlEntityType.Elements("{http://schemas.microsoft.com/ado/2009/02/edm/ssdl}Property").Where(
                        x => x.Attribute("Name").Value == propertyName).First();

                ssdlProperty.SetAttributeValue("StoreGeneratedPattern", storeGeneratedPatternValue);
                /*
                if (ssdlProperty.Attribute("StoreGeneratedPattern") == null ||
                    ssdlProperty.Attribute("StoreGeneratedPattern").Value != "Computed")
                {
                    ssdlProperty.SetAttributeValue("StoreGeneratedPattern", "Computed");
                }
                */
            }
        }
    }
}
